﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;

using Core.Common;

namespace TickMovingSvc
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        Timer timer_30Min;
        protected override void OnStart(string[] args)
        {
            //初始化定时器
            timer_30Min = new System.Timers.Timer(30 * 60 * 1000);
            timer_30Min.Elapsed += onTimer_1Min;
            timer_30Min.AutoReset = true;
            timer_30Min.Enabled = true;
        }

        //到固定时间启动程序
        void onTimer_1Min(object sender, System.Timers.ElapsedEventArgs e)
        {
            //判断非交易时段
            bool isNotTradingTime = false;
            int timeNumber = DateTime.Now.Hour * 100 + DateTime.Now.Minute;
            switch (DateTime.Now.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    if (timeNumber < 600
                        || (timeNumber > 1600 && timeNumber < 1800))
                    {
                        isNotTradingTime = true;
                    }
                    break;
                case DayOfWeek.Tuesday:
                case DayOfWeek.Wednesday:
                case DayOfWeek.Thursday:
                case DayOfWeek.Friday:
                    if ((timeNumber > 300 && timeNumber < 600)
                        || (timeNumber > 1600 && timeNumber < 1800))
                    {
                        isNotTradingTime = true;
                    }
                    break;
                case DayOfWeek.Saturday:
                    if (timeNumber > 300)
                    {
                        isNotTradingTime = true;
                    }
                    break;
                default:
                    isNotTradingTime = true;
                    break;
            }

            //判断是否存在，不存在则启动
            if (isNotTradingTime)
            {
                try
                {
                    bool hasProcess = false;
                    System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();
                    foreach (System.Diagnostics.Process process in processList)
                    {
                        if (process.ProcessName == "BgTickMoving")
                        {
                            hasProcess = true;
                        }
                    }
                    if (!hasProcess)
                    {
                        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Debug\BgTickMoving");
                        System.Diagnostics.Process.Start(path);
                    }
                }
                catch (Exception ex)
                {
                    LogTextHelper.WriteLine("启动搬运程序错误：" + ex.Message);
                }
            }
        }

        protected override void OnStop()
        {
        }
    }
}
